﻿using System;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;

namespace eLearning.MahasiswaView
{
    public partial class TugasPage : PhoneApplicationPage
    {
        private Perkuliahan kuliah;
        private Tugas selectedTugas;

        public TugasPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DataContextPage_Loaded);
        }

        private void DataContextPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey(Config.KULIAH))
            {
                kuliah = (Perkuliahan)PhoneApplicationService.Current.State[Config.KULIAH];
            }
            if (kuliah == null)
            {
                kuliah = new Perkuliahan();
                kuliah.Mhs = (Mahasiswa)SavedUser.Instance.User.ConvertUser();
            }

            LoadTugasList();
        }

        private void LoadTugasList()
        {
            listTugas.ItemsSource = null;
            selectedTugas = null;

            string url = string.Format("{0}&{1}", Config.Instance.TUGAS_SERVICE_URL() + Config.VIEW_ACTION_KEY + "TugasMhs", Guid.NewGuid().ToString());
            string json = JsonConvert.SerializeObject(kuliah);

            Uri uri = new Uri(url, UriKind.Absolute);
            WebClient webClient = new WebClient();

            webClient.Encoding = Encoding.UTF8;
            webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
            webClient.Headers[HttpRequestHeader.ContentLength] = json.Length.ToString();

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(listTugas_UploadStringCompleted);
            try
            {
                webClient.UploadStringAsync(uri, "POST", json);
            }
            catch (Exception) { }
        }

        private void listTugas_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                TugasList list = JsonConvert.DeserializeObject<TugasList>(e.Result);
                if (list.Count > 0)
                    listTugas.ItemsSource = list;
                else
                    listTugas.ItemsSource = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void backButton_Click(object sender, System.EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void listTugas_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                selectedTugas = (Tugas)e.AddedItems[0];
                ((ListBox)sender).SelectedIndex = -1;


                Utility.GotoPage(this, "/View/Mahasiswa/TugasdetailPage.xaml");

                PhoneApplicationService.Current.State[Config.TUGAS] = selectedTugas;
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            LoadTugasList();
        }

    }
}